#import "FlutterPluginPrinterPlugin.h"

#import "BRPtouchPrinterKit/BRPtouchPrinterKit.h"
@implementation FlutterPluginPrinterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_plugin_printer"
            binaryMessenger:[registrar messenger]];
  FlutterPluginPrinterPlugin* instance = [[FlutterPluginPrinterPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"searchForPrinter" isEqualToString:call.method]) {
//      [self searchForPrinter];

    result(@"wilson-- search printer start");
  } else if([@"printImage" isEqualToString:call.method]){
    //获取路径对应的图片文件
    UIImage *image = [UIImage imageWithContentsOfFile:path];
  //    [self printImage:""];
    result(@"wilson -- printImage start");
  }else {
    result(FlutterMethodNotImplemented);
  }
}
//
//Pager Size :62mmRB
//Orientation :Portrate
//Scaling Mode :Fit to pager
//Scaling Factor(for Custom): 1.0
//Paper Align :Pager Center
//Auto Cut:on
//Cut At End :on
- (void)printImage:(UIImage *)image {
    // Specify printer
    BRPtouchPrinter *printer = [[BRPtouchPrinter alloc] initWithPrinterName:@"RJ-4230B" interface:CONNECTION_TYPE_BLUETOOTH];
    [printer setupForBluetoothDeviceWithSerialNumber:@"your-printer-serial-number"];

    // Print Settings
    BRPtouchPrintInfo *settings = [BRPtouchPrintInfo new];
    settings.strPaperName = @"CUSTOM";
    settings.nPrintMode = PRINT_FIT_TO_PAPER;
    settings.scaleValue = 1.0;
//    settings.i = false;
    [printer setPrintInfo:settings];
    [printer setCustomPaperFile:@"path-to-file"];

    // Connect, then print
    if ([printer startCommunication]) {
        int errorCode = [printer printImage:image.CGImage copy:1];
        if (errorCode != ERROR_NONE_) {
            NSLog(@"ERROR - %d", errorCode);
        }
        [printer endCommunication];
    }
}


-(void)searchForPrinter{
    
    // Start search.
       BRPtouchBLEManager *ptouchManager = [BRPtouchBLEManager sharedManager];
       [ptouchManager startSearchWithCompletionHandler:^(BRPtouchDeviceInfo *deviceInfo) {
           if (deviceInfo && deviceInfo.strModelName) {
               NSLog(@"wilson ---- strModelName %@",deviceInfo.strPrinterName);
//               if ([supportPrintLists containsObject:deviceInfo.strModelName]) {
////                   [deviceList addObject:deviceInfo];
//               }
           }
       }];
    
//    BRPtouchBLEManager *ptouchManager = [BRPtouchBLEManager alloc];
//    if(ptouchManager){
//        NSLog(@"have ptouch");
//    }else{
//        NSLog(@"no ptouch");
//    }
//
//    BOOL started = [ptouchManager startSearchWithCompletionHandler:^(BRPtouchDeviceInfo *deviceInfo) {
//        NSLog(@"Model: %@, Local Name: %@", deviceInfo.strModelName, deviceInfo.strBLEAdvertiseLocalName);
//    }];
//
//    NSLog(@"%i",started);
}

@end
